﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.BankModules
{
    public class BankUpdaterRegister
    {
        public NormalDataUpdater<BankModel> GetBankUpdater()
        {
            NormalDataUpdater<BankModel> systemDataUpdater = new NormalDataUpdater<BankModel>();
            systemDataUpdater.DataDeleting += CheckEmployee;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            return systemDataUpdater;
        }
        private void CheckEmployee(object sender, DataValidationEventArgs<BankModel> e)
        {
            EmployeeFetcher employeeFetcher = new EmployeeFetcher();
            string message;
            int rows;
            IList<EmployeeModel> employeeModels = employeeFetcher.List(e.Delete.Site, Lor.OrderBy.GetDefault(), Lor.Limit.GetDefault(), out message,out rows, Lor.Restrain.Eq("Bank", e.Delete.AutoId));
            if(employeeModels.Count>0)
            {
                message = "该银行已被使用";
                e.Cancle = true;
                e.Message = message;
            }
                    
        }
        private void SingletonValid(object sender, DataValidationEventArgs<BankModel> e)
        {
            string message;
            BankFetcher bankFetcher = new BankFetcher();
            BankModel bank = bankFetcher.GetByCode(e.Insert.Site, e.Insert.Code, out message);
            if (bank != null && bank.AutoId != e.Insert.AutoId)
            {
                e.Message = String.Format("单号{0}已经被使用", e.Insert.Code);
                e.Cancle = true;
            }
        }
        private void DataNotSpaceValid(object sender, DataValidationEventArgs<BankModel> e)
        {
            if (String.IsNullOrWhiteSpace(e.Insert.Code))
            {
                e.Message = "单号不允许为空白";
                e.Cancle = true;
                return;
            }
            
        }
    }
}
